#include "stdafx.h"
#include "VCTree.h"
#include "VCTreeCtl.h"
#include "VCTreePpg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNCREATE(CVCTreeCtrl, COleControl)

/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CVCTreeCtrl, COleControl)
	//{{AFX_MSG_MAP(CVCTreeCtrl)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CVCTreeCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CVCTreeCtrl)
	DISP_FUNCTION(CVCTreeCtrl, "GetTree", GetTree, VT_DISPATCH, VTS_NONE)
	DISP_FUNCTION(CVCTreeCtrl, "CreateImageList", CreateImageList, VT_DISPATCH, VTS_NONE)
	DISP_FUNCTION(CVCTreeCtrl, "GetToolBar", GetToolBar, VT_DISPATCH, VTS_NONE)
	DISP_FUNCTION(CVCTreeCtrl, "SetContextMenu", SetContextMenu, VT_EMPTY, VTS_I4)
  DISP_FUNCTION(CVCTreeCtrl, "Invalidate",     _Invalidate, VT_EMPTY, VTS_NONE)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CVCTreeCtrl, COleControl)
	//{{AFX_EVENT_MAP(CVCTreeCtrl)
	EVENT_CUSTOM("MenuCommand", FireMenuCommand, VTS_I4)
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CVCTreeCtrl, 1)
	PROPPAGEID(CVCTreePropPage::guid)

END_PROPPAGEIDS(CVCTreeCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CVCTreeCtrl, "VCTree.VCTreeCtrl.1",
	0x4ada233, 0xfe41, 0x4c6f, 0x9b, 0x95, 0x52, 0x47, 0xb7, 0xdd, 0x32, 0x6)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CVCTreeCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DVCTree =
		{ 0xe31eb7dc, 0x607e, 0x4c0a, { 0x8b, 0x73, 0xe9, 0xc0, 0xff, 0xa6, 0xf5, 0xb1 } };
const IID BASED_CODE IID_DVCTreeEvents =
		{ 0xa0936074, 0xb753, 0x449d, { 0x9d, 0xd1, 0xba, 0x14, 0x88, 0xc0, 0xd7, 0x9d } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwVCTreeOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CVCTreeCtrl, IDS_VCTREE, _dwVCTreeOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl::CVCTreeCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CVCTreeCtrl

BOOL CVCTreeCtrl::CVCTreeCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_VCTREE,
			IDB_VCTREE,
			afxRegApartmentThreading,
			_dwVCTreeOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl::CVCTreeCtrl - Constructor
#pragma warning(disable:4355) // warning C4355: 'this' : used in base member initializer list
CVCTreeCtrl::CVCTreeCtrl() :
  m_TreeDlg( *this )
{
	InitializeIIDs(&IID_DVCTree, &IID_DVCTreeEvents);

	// TODO: Initialize your control's instance data here.
}
#pragma warning(default:4355) //warning C4355: 'this' : used in base member initializer list

/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl::~CVCTreeCtrl - Destructor

CVCTreeCtrl::~CVCTreeCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl::OnDraw - Drawing function

void CVCTreeCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
   if(m_TreeDlg.m_hWnd)
   {
     m_TreeDlg.MoveWindow(rcBounds, TRUE);
     m_TreeDlg.OnSize( 0, rcBounds.Width(), rcBounds.Height() );
   } 
}


/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl::DoPropExchange - Persistence support

void CVCTreeCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);
}


/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl::OnResetState - Reset control to default state

void CVCTreeCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CVCTreeCtrl message handlers


//-----------------------------------------------------------------------------
/// 
// ---
int CVCTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
  m_TreeDlg.Create(IDD_TREEDLG, this);
  return 0;
}


//-----------------------------------------------------------------------------
/// 
// ---
void CVCTreeCtrl::OnSetFocus(CWnd* pOldWnd) 
{
	COleControl::OnSetFocus(pOldWnd);
	m_TreeDlg.SetFocus();
}


//-----------------------------------------------------------------------------
/// 
// ---
LPDISPATCH CVCTreeCtrl::GetTree() 
{
	return m_TreeDlg.GetTree();
}


//-----------------------------------------------------------------------------
/// 
// ---
LPDISPATCH CVCTreeCtrl::CreateImageList() 
{
	return m_TreeDlg.CreateImageList();
}


//-----------------------------------------------------------------------------
/// 
// ---
LPDISPATCH CVCTreeCtrl::GetToolBar() 
{
	return m_TreeDlg.GetToolBar();
}

//-----------------------------------------------------------------------------
/// 
// ---
void CVCTreeCtrl::SetContextMenu(long val) 
{
  m_TreeDlg.SetContextMenu( val );
}


//-----------------------------------------------------------------------------
//
// ---
void CVCTreeCtrl::_Invalidate()
{
  m_TreeDlg.Invalidate();
}
